<?php

namespace common\Service;

use Yii;
use common\Service\Service;
require_once __DIR__.'/../../vendor'."/jieba-php/src/vendor/multi-array/MultiArray.php";
require_once __DIR__.'/../../vendor'."/jieba-php/src/vendor/multi-array/Factory/MultiArrayFactory.php";
require_once __DIR__.'/../../vendor'."/jieba-php/src/class/Jieba.php";
require_once __DIR__.'/../../vendor'."/jieba-php/src/class/Finalseg.php";
require_once __DIR__.'/../../vendor'."/jieba-php/src/class/JiebaAnalyse.php";

use Fukuball\Jieba\Jieba;
use Fukuball\Jieba\Finalseg;
use Fukuball\Jieba\JiebaAnalyse;

ini_set('memory_limit', '1024M');

class JiebaService extends Service
{

    public function __construct()
    {
        Jieba::init();
        Finalseg::init();
        Jieba::loadUserDict(__DIR__.'/../../vendor/jieba-php/src/dict/user_dict.txt');
    }

    /*
     * 搜索引擎关键词提取
     */
    public  function extractTags($string = '',$top = 10){

        $rs = Jieba::cut($string,true);
        print_r($rs);
        $rs = array_unique($rs);
        print_r($rs);
        foreach ($rs as $key => $v){
            echo mb_strlen($v),',';
            if(mb_strlen($v) == 1) unset($rs[$key]);
        }
        print_r($rs);
        exit;

        return $rs;
    }

    public function getWords($string = '',$top = 10){
        if(empty($string)) return [];

        $string = trim($string);
        $strLength = mb_strlen($string);
        $strLength > 100 && $string = mb_substr($string,0,100);

        $words = Jieba::cut($string,true);
        $words = array_unique($words);
        foreach ($words as $key => $word){
            if(mb_strlen($word) == 1) unset($words[$key]);
        }
        if(count($words) > 10) $words = array_slice($words,0,10);

        return $words;
    }



}